<?php
/**
 * Form
 * Класс Form отвечает за прорисовку форм
 * @author Sayp
 */
class Form {
	
	private $objModel; // Модель, чье форму отображаем
	
	public function __construct($objModel = null) {
		$this->objModel = $objModel;
	}
	
	/**
	 * Открываем форму
	 * @param array $arrSettings - массив параметров
	 */
	public function show(array $arrSettings = array()) {
		if (!isset($arrSettings['method'])) $arrSettings['method'] = 'POST';
		$strSetting = $this->getStrSetting($arrSettings);
		
		echo '<form' . $strSetting . '>';
	}
	
	/**
	 * Закрываем форму
	 */
	public function close() {
		if ($this->objModel && $this->objModel->id) echo $this->hidden(array('name' => $this->objModel->getPkName()));
		echo '</form>';
	}
	
	/**
	 * Прорисовываем текстовое поле
	 * @param array $arrSettings - массив параметров
	 */
	public function textField(array $arrSettings = array()) {
		if ($arrSettings['name'] && $this->objModel && $this->objModel->id) $arrSettings['value'] = $this->objModel->getField($arrSettings['name']);
		$strSetting = $this->getStrSetting($arrSettings);
		echo '<input type="text"' . $strSetting . '>';
	}
	
	/**
	 * Прорисовываем текстовое поле
	 * @param array $arrSettings - массив параметров
	 */
	public function textarea(array $arrSettings = array()) {
		if (isset($arrSettings['value'])) {
			$value = $arrSettings['value'];
			unset($arrSettings['value']);
		}
		if ($arrSettings['name'] && $this->objModel && $this->objModel->id) $value = $this->objModel->getField($arrSettings['name']);
		
		$strSetting = $this->getStrSetting($arrSettings);
		echo '<textarea' . $strSetting . '>' . (isset($value) ? $value : '') . '</textarea>';
	}

	/**
	 * Формируем hidden-полу
	 * @param array $arrSettings - массив параметров
	 */
	public function hidden(array $arrSettings = array()) {
		if ($arrSettings['name'] && $this->objModel && $this->objModel->id) $arrSettings['value'] = $this->objModel->getField($arrSettings['name']);
		$strSetting = $this->getStrSetting($arrSettings);
		echo '<input type="hidden"' . $strSetting . '>';
	}
	
	/**
	 * Отображаем submit формы
	 * @param array $arrSetting - массив параметров
	 */
	public function submit(array $arrSetting = array()) {
		if (isset($arrSetting['value'])) {
			$value = $arrSetting['value'];
			unset($arrSetting['value']);
		}
		else $value = 'Отправить';
		
		if (isset($arrSetting['class'])) {
			$class = $arrSetting['class'];
			unset($arrSetting['class']);
		}
		else $class = 'btn btn-red';
		
		echo '<button type="submit" class="' . $class . '">' . $value . '</button>';
	}
	
	/**
	 * Отображаем select
	 * @param array $arrSettings - массив параметров
	 */
	public function select($arrSettings = array()) {
		if (empty($arrSettings['arr_select'])) return false;
		
		$arrSelect = $arrSettings['arr_select'];
		unset($arrSettings['arr_select']);
		
		if (isset($arrSettings['value'])) {
			$value = $arrSettings['value'];
			unset($arrSettings['value']);
		}
		if ($arrSettings['name'] && $this->objModel && $this->objModel->id) $value = $this->objModel->getField($arrSettings['name']);
		$strSetting = $this->getStrSetting($arrSettings);
		
		echo '<select' . $strSetting . '>';
			foreach ($arrSelect as $optionValue => $optionName) {
				echo '<option value="' . $optionValue . '"' . (isset($value) && $value == $optionValue ? ' selected="selected"' : null) . '>' . $optionName . '</option>';
			}
		echo '</select>';
	}
	
	/**
	 * Отображаем checkbox
	 * @param array $arrSettings - массив параметров
	 */
	public function checkbox($arrSettings = array()) {
		$value = 0;
		
		if (isset($arrSettings['value'])) {
			$value = $arrSettings['value'];
			unset($arrSettings['value']);
		}
		
		if ($arrSettings['name'] && $this->objModel && $this->objModel->id) $value = $this->objModel->getField($arrSettings['name']);
		$strSetting = $this->getStrSetting($arrSettings);
	
		echo '<input type="checkbox"' . ($value ? ' checked="checked"' : null) . $strSetting . '>';
	}
	
	/**
	 * Формируем строку настроей
	 * @param array $arrSettings - массив параметров
	 * @return string $strSetting - строка настроек
	 */
	private function getStrSetting(array $arrSettings) {
		$strSetting = '';
		$i = 0;
		foreach ($arrSettings as $settingName => $settingValue) {
			
			switch ($settingName) {
				case 'name':
					if ($this->objModel) $settingValue = get_class($this->objModel) . '[' . $settingValue . ']';
					break;
			}
			
			$strSetting .= ' ' . $settingName . '="' . $settingValue . '"';
		}
		
		return $strSetting;
	}
	
}